package com.security.door.controller.accessManage;

import com.alibaba.fastjson.JSONObject;
import com.security.door.comm.TreeDataBuilder;
import com.security.door.core.wrapper.WrapMapper;
import com.security.door.core.wrapper.Wrapper;
import com.security.door.model.domain.baseRoleAuth;
import com.security.door.model.vo.treeDataVo;
import com.security.door.service.IbaseRoleAuthService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(value = "WEB - baseRoleAuthController", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class baseRoleAuthController {

    @Autowired
    HttpSession session;
    @Autowired
    HttpServletRequest httpServletRequest;
    @Autowired
    IbaseRoleAuthService ibaseRoleAuthService;

    @GetMapping(value = "/roleAuth/selectAuth")
    public Wrapper selectAuth(@RequestParam String JsonData) {
        List<baseRoleAuth> result = ibaseRoleAuthService.selectAuth(JsonData);
        return WrapMapper.ok("操作成功");
    }

    @GetMapping(value = "/roleAuth/selectTree/{roleCode}")
    public Wrapper<List<treeDataVo>> selectTree(@PathVariable("roleCode") String roleCode) {
        List<treeDataVo> list = ibaseRoleAuthService.selectTree(roleCode);
        TreeDataBuilder builder = new TreeDataBuilder(list);
        return WrapMapper.ok(builder.buildJSONTree());
    }

    @GetMapping(value = "/roleAuth/selectAllTree")
    @ApiOperation(value = "查询所有角色信息", notes = "查询所有角色信息")
    public Wrapper<List<treeDataVo>> selectAllTree() {
        Map<String, String> map = new LinkedHashMap<>();
        List<treeDataVo> list = ibaseRoleAuthService.selectAllTree(null);
        list.removeIf(x -> x.getPid() == null);
        TreeDataBuilder builder = new TreeDataBuilder(list);
        return WrapMapper.ok(builder.buildJSONTree());
    }

    @PostMapping(value = "/roleAuth/insertAuth")
    public Wrapper insertAuth(@RequestBody List<baseRoleAuth> list) {
        ibaseRoleAuthService.deleteByRoleCode(list.get(0).getRolecode());
        ibaseRoleAuthService.insertAuth(list);
        return WrapMapper.ok("操作成功");
    }

    @PostMapping(value = "/roleAuth/deleteByMenuId")
    public Wrapper deleteByMenuId(@RequestBody baseRoleAuth entity) {
        int result = ibaseRoleAuthService.deleteByMenuId(entity);
        return WrapMapper.ok("操作成功");
    }

    @GetMapping(value = "/roleAuth/deleteMore")
    public Wrapper deleteMore(@RequestParam String JsonData) {
        JSONObject data = JSONObject.parseObject(JsonData);
        Map<String, String> map = new HashMap<>();
        map.put("rolecode", data.getString("rolecode"));
        map.put("menuidlist", data.getString("list"));
        int result = ibaseRoleAuthService.deleteMore(map);
        return WrapMapper.ok("操作成功");
    }
}
